package com.ll.wechat.base.fragment;

import com.ll.wechat.base.dao.IBaseActivityManager;
import com.ll.wechat.base.utils.ActivityManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Fragment的基础类
 * 
 * @ClassName: BaseFragment
 * @author yeliangliang
 * @date 2015-7-25 下午5:44:11
 */
public abstract class BaseFragment extends Fragment implements IBaseActivityManager {
	protected View rootView;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(setRootViewId(), container, false);
		initView(rootView);
		initData();
		return rootView;
	}

	/**
	 * 设置根布局的资源id
	 * 
	 * @return
	 */
	public abstract int setRootViewId();

	/**
	 * 初始化组件
	 */
	protected abstract void initView(View rootView);

	/**
	 * 初始化数据
	 */
	protected abstract void initData();

	@Override
	public void finishActivity(Activity activity) {
		ActivityManager.list.remove(activity);
		activity.finish();
	}

	@Override
	public void skipActivity(Activity oldActivity, Class<?> newActivity) {
		showActivity(oldActivity, newActivity);
		ActivityManager.list.remove(oldActivity);
		oldActivity.finish();
	}

	@Override
	public void showActivity(Activity oldActivity, Class<?> newActivity) {
		Intent intent = new Intent(oldActivity, newActivity);
		startActivity(intent);
	}

	@Override
	public void showActivity(Activity oldActivity, Class<?> newActivity, Bundle bundle) {
		Intent intent = new Intent(oldActivity, newActivity);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void showActivityForResult(Activity oldActivity, Class<?> newActivity, int requestCode) {
		Intent intent = new Intent(oldActivity, newActivity);
		startActivityForResult(intent, requestCode);
	}

	@Override
	public void showActivityForResult(Activity oldActivity, Class<?> newActivity, Bundle bundle,
			int requestCode) {
		Intent intent = new Intent(oldActivity, newActivity);
		intent.putExtras(bundle);
		startActivityForResult(intent, requestCode);
	}
}
